//
//  CLReadMainController.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/8/31.
//

#import "CLReadMainController.h"
#import "CLReadTextParser.h"
#import "CLReadTextFastParser.h"
#import "CLReadController.h"
@interface CLReadMainController ()

@end

@implementation CLReadMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 全本解析
    UIButton *fullButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [fullButton setTitle:@"全本解析" forState:UIControlStateNormal];
    fullButton.backgroundColor = [UIColor greenColor];
    [fullButton addTarget:self action:@selector(fullRead) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fullButton];
    fullButton.frame = CGRectMake((SCREENWIDTH / 2 - CL_SPACE_SA_100) / 2, SCREENWIDTH - CL_SPACE_SA_150, CL_SPACE_SA_100, CL_SPACE_SA_100);
    
    // 快速解析阅读
    UIButton *fastButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [fastButton setTitle:@"快速解析" forState:UIControlStateNormal];
    fastButton.backgroundColor = [UIColor greenColor];
    [fastButton addTarget:self action:@selector(fastRead) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fastButton];
    fastButton.frame = CGRectMake(SCREENWIDTH - CL_SPACE_SA_100 - (SCREENWIDTH / 2 - CL_SPACE_SA_100) / 2, SCREENHEIGHT - CL_SPACE_SA_150, CL_SPACE_SA_100, CL_SPACE_SA_100);
}
// 全本解析阅读
- (void)fullRead {
    WEAK_SELF;
    NSLog(@"缓存文件地址:%@", HomePath);
    NSLog(@"全本解析(第一次进入)速度慢");
  
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"球墨2" withExtension:@"txt"];
    [SVProgressHUD showLoading];
    NSLog(@"全本解析开始");
    [CLReadTextParser parser:url completion:^(CLReadModel * _Nonnull readModel) {
        [SVProgressHUD dismiss];
        NSLog(@"全本解析结束");
        if (readModel == nil) {
            NSLog(@"全本解析失败");
            return;
        }
        CLReadController *vc = [[CLReadController alloc]init];
        vc.viewModel.prefersNavigationBarHidden = YES;
        vc.readModel = readModel;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)fastRead {
    WEAK_SELF;
    NSLog(@"缓存文件地址:%@", HomePath);
    
    [SVProgressHUD showLoading];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"球墨2" withExtension:@"txt"];
    NSLog(@"快速解析开始");
    [CLReadTextFastParser parser:url completion:^(CLReadModel * _Nonnull readModel) {
        [SVProgressHUD dismiss];
        NSLog(@"全本解析结束");
        if (readModel == nil) {
            NSLog(@"全本解析失败");
            return;
        }
        CLReadController *vc = [[CLReadController alloc]init];
        vc.viewModel.prefersNavigationBarHidden = YES;
        vc.readModel = readModel;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}
@end
